package com.huike.pmps.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huike.pmps.model.entity.POCourseSupplierDetail;
import com.huike.pmps.model.entity.SupplierSettlementDetail;
import com.huike.pmps.model.vo.supplier.SupplierPoVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */

public interface SupplierSettlementDetailDao extends BaseMapper<SupplierSettlementDetail> {

    //批量更新记录状态
    @Update({
            "<script>",
            "<foreach collection='list' item='item' index='index' separator=';'>",
            "update p_supplier_settlement_detail",
            "set state = #{item.state}  where id= #{item.id}",
            "</foreach>",
            "</script>"
    })
    int updateStateById(List<SupplierSettlementDetail> list);


    @Update({
            "<script>",
            "UPDATE p_po_course SET  supplier_paid_amount=(SELECT SUM(supplier_paid_amount) FROM p_supplier_settlement_detail WHERE po_course_id=#{poCourseId})  WHERE id=#{poCourseId};					                ",
            "UPDATE p_po_course SET  supplier_unpaid_amount=(estimated_generation_cost-supplier_paid_amount)  WHERE id=#{poCourseId};                                                                                       ",
            "UPDATE p_po_course_supplier_detail SET supplier_paid_amount=(SELECT supplier_paid_amount FROM p_supplier_settlement_detail WHERE po_course_supplier_id=#{poCourseSupplierId}) WHERE id=#{poCourseSupplierId};	",
            "UPDATE p_po_course_supplier_detail SET supplier_unpaid_amount=(estimated_generation_cost-supplier_paid_amount)  WHERE id=#{poCourseSupplierId};                                                                ",
            "</script>"
    })
    int updatePOCourseAndCourseSupplierPaidAmount(Integer poCourseId, Integer poCourseSupplierId);

    /**
     * 获取供应商当前承接已经承接课程的分钟数 课程数量*60
     *
     * @param supplierId
     * @return
     */
    @Select("select sum(quantity*60) from `p_supplier_settlement_detail` where  supplier_id=#{supplierId} and state=0 and service_type_name in ('制作','整包')")
    Integer selectSupplierMinuteOfCourses(Integer supplierId);


    @Insert({
            "<script>",
            " insert into p_supplier_settlement_detail ( ",
            "   po_id,                                   ",
            "   po_course_id,                            ",
            "   po_course_supplier_id,                   ",
            "   po,                                      ",
            "   po_contract_amount,                      ",
            "   customer_name,                           ",
            "   region_id,                               ",
            "   region_name,                             ",
            "   province_id,                             ",
            "   province_name,                           ",
            "   course_charge_id,                        ",
            "   course_charge_name,                      ",
            "   course_id,                               ",
            "   course_name,                             ",
            "   subject_category_id,                     ",
            "   subject_category_name,                   ",
            "   major_category,                          ",
            "   shoot_mode_id,                           ",
            "   shoot_mode_name,                         ",
            "   supplier_type_id,                        ",
            "   supplier_type_name,                      ",
            "   supplier_id,                             ",
            "   supplier_name,                           ",
            "   service_type_id,                         ",
            "   service_type_name,                       ",
            "   service_name,                            ",
            "   estimated_settlement_date,               ",
            "   unit_price,                              ",
            "   quantity,                                ",
            "   estimated_generation_cost,               ",
            "   supplier_paid_amount,                    ",
            "   supplier_unpaid_amount,                  ",
            "   contract_finished_time_length,           ",
            "   actual_unit_price,                       ",
            "   actual_quantity,                         ",
            "   other_expenses,                          ",
            "   actual_total_cost,                       ",
            "   actual_time_length,                      ",
            "   payment_state_id,                        ",
            "   payment_state_name,                      ",
            "   invoice_type_id,                         ",
            "   invoice_type_name,                       ",
            "   tax_point,                               ",
            "   final_evaluation,                        ",
            "   description,                             ",
            "   shoot_source_material,                   ",
            "   project_filing,                          ",
            "   pure_filing,                             ",
            "   filing_date,                             ",
            "   settlement_date,                         ",
            "   acceptance_report_path,                  ",
            "   import_flag,                             ",
            "   import_service_id,                       ",
            "   state,                                   ",
            "   row_edit,                                ",
            "   update_time,                             ",
            "   create_time                              ",
            " ) values                                   ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            " (									",
            "  #{item.poId},                      ",
            "  #{item.poCourseId},                ",
            "  #{item.poCourseSupplierId},        ",
            "  #{item.po},                        ",
            "  #{item.poContractAmount},          ",
            "  #{item.customerName},              ",
            "  #{item.regionId},                  ",
            "  #{item.regionName},                ",
            "  #{item.provinceId},                ",
            "  #{item.provinceName},              ",
            "  #{item.courseChargeId},            ",
            "  #{item.courseChargeName},          ",
            "  #{item.courseId},                  ",
            "  #{item.courseName},                ",
            "  #{item.subjectCategoryId},         ",
            "  #{item.subjectCategoryName},       ",
            "  #{item.majorCategory},             ",
            "  #{item.shootModeId},               ",
            "  #{item.shootModeName},             ",
            "  #{item.supplierTypeId},            ",
            "  #{item.supplierTypeName},          ",
            "  #{item.supplierId},                ",
            "  #{item.supplierName},              ",
            "  #{item.serviceTypeId},             ",
            "  #{item.serviceTypeName},           ",
            "  #{item.serviceName},               ",
            "  #{item.estimatedSettlementDate},   ",
            "  #{item.unitPrice},                 ",
            "  #{item.quantity},                  ",
            "  #{item.estimatedGenerationCost},   ",
            "  #{item.supplierPaidAmount},        ",
            "  #{item.supplierUnpaidAmount},      ",
            "  #{item.contractFinishedTimeLength},",
            "  #{item.actualUnitPrice},           ",
            "  #{item.actualQuantity},            ",
            "  #{item.otherExpenses},             ",
            "  #{item.actualTotalCost},           ",
            "  #{item.actualTimeLength},          ",
            "  #{item.paymentStateId},            ",
            "  #{item.paymentStateName},          ",
            "  #{item.invoiceTypeId},             ",
            "  #{item.invoiceTypeName},           ",
            "  #{item.taxPoint},                  ",
            "  #{item.finalEvaluation},           ",
            "  #{item.description},               ",
            "  #{item.shootSourceMaterial},       ",
            "  #{item.projectFiling},             ",
            "  #{item.pureFiling},                ",
            "  #{item.filingDate},                ",
            "  #{item.settlementDate},            ",
            "  #{item.acceptanceReportPath},      ",
            "  #{item.importFlag},                ",
            "  #{item.importServiceId},           ",
            "  #{item.state},                     ",
            "  #{item.rowEdit},                   ",
            "  #{item.updateTime},                ",
            "  #{item.createTime}                 ",
            " )                                   ",
            "</foreach>",
            "</script>"
    })
    int insertBatchList(List<SupplierSettlementDetail> list);


    /**
     * 计算 p_supplier_settlement_detail 每条PO下 预估总和 已支付总和 未支付总和
     */
    @Select("select pssd.po,IFNULL(sum(pssd.estimated_generation_cost),0.00) as 'estimatedGenerationCostSum', IFNULL(sum(pssd.supplier_paid_amount),0.00) as 'paidAmountSum', IFNULL(sum(pssd.supplier_unpaid_amount),0.00) as 'unpaidAmountSum'from `p_supplier_settlement_detail` pssd  GROUP BY pssd.po ")
    List<SupplierPoVo> selectSumCostAndPaidByPo();


}
